﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Bussines;
using System.Data.SqlClient;
using System.IO;

namespace ownx
{
    public partial class wfPartnerChoice : System.Web.UI.Page
    {
        clPARTNER_Choice _clPARTNER;
        DataSet dsPARTNER;
        clTIPUS_LIST _clTipusok;
        TBLObj blObj = null;

        int _USR_ID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Session["BL"] != null)
            {
                blObj = (TBLObj)Session["BL"];
            }
            //TBLObj blObj = new TBLObj("Test", Session["CONSTR"].ToString(), (clFIELDINFO_LIST)Session["FIELDINFO"]);
            _USR_ID = Convert.ToInt32(Session["USR_ID"].ToString());
            _clPARTNER = new clPARTNER_Choice(blObj, _USR_ID);
            _clTipusok = new clTIPUS_LIST(blObj, "PRT");
                        

            //ha ez az első meghívás
            if (!(Page.IsPostBack))
            {

                dsPARTNER = _clPARTNER.GetPARTNER("",false);
                FillCombos();
                DataInit();
            }
            else
            {
                if (UjFilter())
                {
                    lPRT_TIPUS.Text = ddlPRT_TIPUS.SelectedValue;
                    lCMB_VAROS.Text = tbCMB_VAROS.Text;
                    lIRS_SZAM.Text = tbIRS_SZAM.Text;
                    lPRT_NEV.Text = tbPRT_NEV.Text;
                    lPRT_NEM.Text = ddlPRT_NEM.SelectedValue;

                    if (cbAla.Checked)
                        lAla.Text = "1";
                    else
                        lAla.Text = "0";


                    string iWhereSQL = "";

                    if (lPRT_TIPUS.Text != "-1")
                        iWhereSQL += "AND PRT_TIPUS = '" + lPRT_TIPUS.Text + "' ";
                    if (lCMB_VAROS.Text != "")
                        iWhereSQL += "AND UPPER(CMB_VAROS) LIKE UPPER('" + lCMB_VAROS.Text + "%') ";
                    if (lIRS_SZAM.Text != "")
                        iWhereSQL += "AND UPPER(IRS_SZAM) LIKE UPPER('" + lIRS_SZAM.Text + "%') ";
                    if (lPRT_NEV.Text != "")
                    {
                        iWhereSQL += "AND (UPPER(PRT_NEV1) LIKE UPPER('" + lPRT_NEV.Text + "%') ";
                        iWhereSQL += "OR UPPER(PRT_NEV2) LIKE UPPER('" + lPRT_NEV.Text + "%') )";
                    }
                    if (lPRT_NEM.Text != "")
                        iWhereSQL += "AND UPPER(isnull(PRT_NEM,'C')) like '" + lPRT_NEM.Text + "' ";
                    lFilter.Text = iWhereSQL;
                    dsPARTNER = _clPARTNER.GetPARTNER(iWhereSQL,cbAla.Checked );
                    DataInit();
                }
                else
                {
                    dsPARTNER = _clPARTNER.GetPARTNER(lFilter.Text, cbAla.Checked);
                }
            }
        }

        private bool UjFilter()
        {
            bool ret = false;
            ret = (!((ddlPRT_NEM.SelectedValue == lPRT_NEM.Text) &&  (ddlPRT_TIPUS.SelectedValue == lPRT_TIPUS.Text) && (lCMB_VAROS.Text == tbCMB_VAROS.Text) && (lIRS_SZAM.Text == tbIRS_SZAM.Text) && (lPRT_NEV.Text == tbPRT_NEV.Text)));
            if (!(ret))
                if (((lAla.Text == "0") && (cbAla.Checked)) || ((lAla.Text == "1") && (!(cbAla.Checked))))
                    ret = true;
            return ret;
        }

        private void DataInit()
        {
            //feltöltöm a detasetet, majd a gridet
          //  dsPARTNER = _clPARTNER.GetPARTNER("");

            gvPARTNER.DataSource = dsPARTNER;
            gvPARTNER.DataBind();

            if (dsPARTNER.Tables["PARTNER"].Rows.Count > 0)
            {
                //kijelölöm az elsőt, és kitöltöm az editeket
                gvPARTNER.SelectedIndex = 0;
               // gvMegjegyzesek_SelectedIndexChanged(gvMegjegyzesek, null);
            }
        }

        private void FillCombos()
        {
            ddlPRT_TIPUS.Items.Clear();
            ListItem li0 = new ListItem("-", "-1");
            ddlPRT_TIPUS.Items.Add(li0);
            
            foreach (clTIPUS akt in _clTipusok.lTIPUS)
            {
                ListItem li = new ListItem(akt.fTPS_MEGNEVEZES, akt.fTPS_TIPUSKOD.ToString());

                ddlPRT_TIPUS.Items.Add(li);

            }
        }

        protected void bUj_Click(object sender, EventArgs e)
        {
            Session["PRT_ID"] = -1;
            Session["PARENT_FORM"] = "wfPartnerChoice.aspx";
            Response.Redirect("wfPartner.aspx");
        }

        protected void bSzerkeszt_Click(object sender, EventArgs e)
        {

            if ((obj_id.Value != "0") && (obj_id.Value != "-1"))//(dsPARTNER.Tables["PARTNER"].Rows.Count > 0)
            {
                Session["PRT_ID"] = Convert.ToInt16(obj_id.Value); //(int)dsPARTNER.Tables["PARTNER"].Rows[gvPARTNER.SelectedIndex]["PRT_ID"];
                Response.Redirect("wfPartner.aspx");
            }
        }

        protected void bUj0_Click(object sender, EventArgs e)
        {
           
        }

        protected void bVissza_Click(object sender, EventArgs e)
        {
            Response.Redirect("wfDefault.aspx");
        }

        public bool ExcelExport(int iPRT_ID, int iAll,ref string oErr)
        {
            try
            {
                DataSet DS = new DataSet();
                SqlDataAdapter Adapter = new SqlDataAdapter();
                Adapter.SelectCommand = new SqlCommand("select * from dbo.FN_PARTNEREXPORT(" + iPRT_ID.ToString() + "," + blObj.USR_ID.ToString() + "," + iAll.ToString() + ")", blObj.sqlConnection);
                Adapter.Fill(DS, "PARTNER_EXP");

                StringWriter sw = new StringWriter();
                HtmlTextWriter wr = new HtmlTextWriter(sw);

                wr.RenderBeginTag(HtmlTextWriterTag.H1);
                wr.Write("Partnerek");
                wr.RenderEndTag();

                GridView grid = new GridView();
                grid.DataSource = DS;
                grid.DataBind();
                grid.HeaderStyle.Font.Bold = true;
                grid.RenderControl(wr);

                this.Response.Clear();
                this.Response.ContentType = "application/vnd.ms-excel";
                this.Response.Write(sw.ToString());
                this.Response.End();
                return true;
            }
            catch (Exception e)
            {
                string s = "Hiba a excel export közben!";
                Utilities.LogError(blObj, e, "wfPartnerChoice", s);
                oErr = s;
                return false;
            }
        }

        protected void bExport_Click(object sender, EventArgs e)
        {
            string Err = "";
            bool RetVal;
            int I=0;
            if (cbAla.Checked)
            { 
                I = 1; 
            }

            RetVal = ExcelExport(-1, I, ref Err);
            if (!RetVal)
            {
                lMessage.Text = Err;
            }
            else
            {
                lMessage.Text = "";
            }
        }

        protected void gvPARTNER_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.ID = e.Row.Cells[0].Text;
                e.Row.Attributes.Add("onclick", "GridView_selectRow(this,'" + e.Row.Cells[0].Text + "')");
                //e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvPARTNER, "Select$" + e.Row.RowIndex);
                //e.Row.Attributes.Add("onclick", "GridView_selectRow(this,'" + e.Row.Cells[0].Text + "')");
                e.Row.Attributes.Add("onmouseover", "GridView_mouseHover(this)");
            }
        }

 



    }
}